//
//  TSWithDrawCashSectionFooterView.swift
//  TSProfitProject
//
//  Created by TMS on 2019/11/5.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit

protocol TSWithDrawCashSectionFooterViewDelegate : NSObjectProtocol {
    func fitToWithdraw(money:String, bankpwd:String)
}

class TSWithDrawCashSectionFooterView: UIView {

    weak var delegate : TSWithDrawCashSectionFooterViewDelegate? = nil
    
    var handleLabel : UILabel!
    var cashField : UITextField!
    var passwordField : UITextField!
    
    override init(frame: CGRect) {
           super.init(frame: frame)
           configViews()
       }
       
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupView(withdrawType:Int) {
        
        switch withdrawType {
        case 0:
            handleLabel.text = String.init(format: "可提现金额：%.2f", TSLoginManager.shared._usermodel!.yingli)
        case 1:
            handleLabel.text = String.init(format: "可提现金额：%.2f", TSLoginManager.shared._usermodel!.yongjin)
        case 2:
            handleLabel.text = String.init(format: "可提现金额：%.2f", TSLoginManager.shared._usermodel!.zhongxin)
        default: break
        }
    }
    
    func checkCommitCondition() -> Bool {
        
        let money = cashField.text?.removeAllSapce as NSString?
        guard money?.doubleValue != 0 else {
            CBToast.showToastAction(message: "请输入取款金额")
            return false
        }
        
        guard passwordField.text?.removeAllSapce.count != 0 else {
            CBToast.showToastAction(message: "请输入取款密码")
            return false
        }
        return true
    }
    
    @objc func commitAction() {
        
        if checkCommitCondition() {
            
            if self.delegate != nil {
                self.delegate?.fitToWithdraw(money: cashField.text!.removeAllSapce, bankpwd: passwordField.text!.removeAllSapce)
            }
        }
    }
    
    func configViews() {
        
        handleLabel = createLabel(text: "可提现金额：0.00")
        self.addSubview(handleLabel)
        handleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(18)
            make.left.equalTo(34)
            make.width.lessThanOrEqualTo(SCREEN_WIDTH - 2*34)
        }
        
        let line1 = drawLine()
        self.addSubview(line1)
        line1.snp.makeConstraints { (make) in
            make.left.equalTo(12)
            make.top.equalTo(handleLabel.snp_bottom).offset(20)
            make.size.equalTo(CGSize(width: SCREEN_WIDTH-2*12, height: 1))
        }
        
        let unitLabel = createLabel(text: "人民币")
        self.addSubview(unitLabel)
        unitLabel.snp.makeConstraints { (make) in
            make.left.equalTo(handleLabel)
            make.top.equalTo(line1.snp.bottom).offset(18)
            make.width.lessThanOrEqualTo(handleLabel)
        }
        
        let line2 = drawLine()
        self.addSubview(line2)
        line2.snp.makeConstraints { (make) in
            make.left.equalTo(line1)
            make.top.equalTo(unitLabel.snp_bottom).offset(20)
            make.size.equalTo(line1)
        }
        
        cashField = createInputField(placeholder: "请输入取款金额")
        cashField.keyboardType = .phonePad
        self.addSubview(cashField)
        cashField.snp.makeConstraints { (make) in
            make.left.equalTo(handleLabel)
            make.top.equalTo(line2.snp_bottom)
            make.size.equalTo(CGSize(width: SCREEN_WIDTH - 2*34, height: 50))
        }
        
        let line3 = drawLine()
        self.addSubview(line3)
        line3.snp.makeConstraints { (make) in
            make.left.equalTo(line1)
            make.top.equalTo(cashField.snp_bottom)
            make.size.equalTo(line1)
        }
        
        passwordField = createInputField(placeholder: "请输入取款密码")
        passwordField.isSecureTextEntry = true
        self.addSubview(passwordField)
        passwordField.snp.makeConstraints { (make) in
            make.left.equalTo(handleLabel)
            make.top.equalTo(line3.snp_bottom)
            make.size.equalTo(cashField)
        }
        
        let line4 = drawLine()
        self.addSubview(line4)
        line4.snp.makeConstraints { (make) in
            make.left.equalTo(line1)
            make.top.equalTo(passwordField.snp_bottom)
            make.size.equalTo(line1)
        }
        
        let footerView = UIView.init()
        footerView.backgroundColor = WHITE_COLOR
        self.addSubview(footerView)
        footerView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(line4.snp_bottom)
            make.height.equalTo(88)
        }
        
        let commitBtn = UIButton.init(type: .system)
        commitBtn.setTitle("提交", for: .normal)
        commitBtn.setTitleColor(WHITE_COLOR, for: .normal)
        commitBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        commitBtn.backgroundColor = BASE_PURPLE_COLOR
        commitBtn.layer.cornerRadius = 24
        commitBtn.layer.masksToBounds = true
        commitBtn.addTarget(self, action: #selector(commitAction), for: .touchUpInside)
        footerView.addSubview(commitBtn)
        commitBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(30)
            make.size.equalTo(CGSize(width: SCREEN_WIDTH*0.682, height: 48))
        }
    }
    
    func createInputField(placeholder:String) -> UITextField {
        
        let string = placeholder
        let attributeStr = NSMutableAttributedString.init(string: string)
        attributeStr.addAttributes([NSAttributedString.Key.foregroundColor:UIColor.hexadecimalColor(hexadecimal: "#989898"), NSAttributedString.Key.font:UIFont.systemFont(ofSize: 14, weight: .medium)], range: NSMakeRange(0, string.count))
        
        let textField = UITextField.init()
        textField.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        textField.textColor = UIColor.hexadecimalColor(hexadecimal: "#282828")
        textField.attributedPlaceholder = (attributeStr.copy() as! NSAttributedString)
        textField.borderStyle = .none
        textField.clearButtonMode = .whileEditing
        return textField
    }

    func createLabel(text:String) -> UILabel {
        
        let label = UILabel.init()
        label.text = text
        label.textColor = UIColor.hexadecimalColor(hexadecimal: "#282828")
        label.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        return label
    }
    
    func drawLine() -> UIView {
        let line = UIView.init()
        line.backgroundColor = UIColor.hexadecimalColor(hexadecimal: "#F5F5F5")
        return line
    }

}
